package com.ccl.iot.dialogs;import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;

import com.ccl.iot.publics.Messager;
import com.ccl.iot.views.ItemGridView;
import com.ccl.iot.R;

public abstract class ItemSelectDialog<TYPE> extends EditDialog {
	public interface OnItemSelectListener<TYPE>{
		public void onItemSelect(TYPE aItem);
	}
	
	private TYPE iDefault = null;
	private OnItemSelectListener<TYPE> iOnItemSelectListener = null;
	
	private ItemGridView<TYPE> iListView = null;
	
	public ItemSelectDialog(Context context, TYPE aDefault, OnItemSelectListener<TYPE> aListener) {
		super(context);
		
		iDefault = aDefault;
		
		iOnItemSelectListener = aListener;
	}
	
	protected abstract ItemGridView<TYPE> getListView();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		iListView = getListView();
	}

	@Override
	protected void onShow() {
		super.onShow();
		
		if (iDefault != null && iListView != null){
			iListView.addCheckedItem(iDefault);
		}
	}
	
	@Override
	protected boolean onOK() {
		if (iListView != null){
			ArrayList<TYPE> tItems = iListView.getCheckedItems();
			
			if (tItems != null && tItems.size() > 0){
				if (iOnItemSelectListener != null){
					iOnItemSelectListener.onItemSelect(tItems.get(0));
				}
				
				return true;
			}else{
				Messager.Show(R.string.Error_NoItemSelect);
			}
		}
		
		return false;
	}
}
